package cn.picclife.mwx.manager.sys.controller;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.PageResponse;
import cn.picclife.mwx.manager.sys.model.bo.SysMaintainReq;
import cn.picclife.mwx.manager.sys.service.SysMaintainVersionService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@RestController
@RequestMapping("version")
public class SysMaintainVersionController {

	private Log log = LogFactory.getLog(SysMaintainVersionController.class);
	
	@Autowired
	private SysMaintainVersionService sysMaintainVersionServiceImpl;
	/**
	 * 增加数据
	 * @param
	 */
	@RequestMapping(value="/saveSysMaintainVersion", method= {RequestMethod.POST})
	public PageResponse saveSysMaintainVersion(HttpServletRequest request, HttpServletResponse response, @RequestParam("uploadFile") MultipartFile file) {
		
		SysMaintainReq req = new SysMaintainReq();
		req.setUploadFile(file);//文件
		req.setId(request.getParameter("id"));
		req.setVersionNo(request.getParameter("versionNo"));//版本号
		req.setVersionUpdateTime(request.getParameter("versionUpdateTime"));//更新时间
		req.setSystemName(request.getParameter("systemName"));//系统名称
		log.info("===========saveSysMaintainVersion系统维护数据接口请求报文："+JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		if(req==null||req.getId()==null||req.getId().length()<=0) {
			pageResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			pageResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========saveSysMaintainVersion系统维护数据接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		try {
			//sysMaintainVersionServiceImpl.saveSysMaintain(req);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========saveSysMaintainVersion系统维护数据接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========saveSysMaintainVersion系统维护数据接口返回报文："+JSONObject.toJSON(pageResponse).toString());
		return pageResponse;
		
	}
	
	/**
	 * 查询
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/querySysMaintain", method = { RequestMethod.POST })
	public PageResponse querySysMaintainVersion(@RequestBody SysMaintainReq req) {

		log.info("===========querySysMaintainVersion查询系统数据量接口请求报文：" + JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		try {
			PageCount pageCount =  sysMaintainVersionServiceImpl.querySysMaintainVersionPage(req);
			pageResponse.setPageCount(pageCount);;
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========querySysMaintainVersion查询系统数据量接口返回报文：" + JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========querySysMaintainVersion查询系统数据量接口返回成功===========");
		return pageResponse;
	}

	/**
	 * 数据更新
	 */
	/*@RequestMapping(value = "/updateSysMaintain", method = { RequestMethod.POST })
	public DataResponse updateSysMaintainVersion(HttpServletRequest request, HttpServletResponse response, @RequestParam("uploadFile") MultipartFile file) {

		SysMaintainReq req = new SysMaintainReq();
		req.setUploadFile(file);//文件
		req.setId(request.getParameter("id"));
		req.setVersionNo(request.getParameter("versionNo"));//版本号
		req.setVersionUpdateTime(request.getParameter("versionUpdateTime"));//更新时间
		req.setSystemName(request.getParameter("systemName"));//系统名称
		req.setSysversionId(request.getParameter("sysversionId"));//versionID
		log.info("===========updateSysMaintainVersion更新系统数据量接口请求报文：" + JSONObject.toJSON(req).toString());
		DataResponse dataResponse = new DataResponse();
		try {
			int sum = sysMaintainVersionServiceImpl.updateSysMaintain(req);
			if(sum==2) {
				dataResponse.setCode(ResponseStatusEnum.NOT_DATA.getCode());
				dataResponse.setMsg(ResponseStatusEnum.NOT_DATA.getName());
				log.info("===========updateSysMaintainVersion更新系统数据量接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
			if (sum == 0) {
				dataResponse.setCode(ResponseStatusEnum.UPDATE_FAIL_CODE.getCode());
				dataResponse.setMsg(ResponseStatusEnum.UPDATE_FAIL_CODE.getName());
				log.info("===========updateSysMaintainVersion更新系统数据量接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
		} catch (Exception e) {
			e.printStackTrace();
			dataResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			dataResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========updateSysMaintainVersion更新系统数据量接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========updateSysMaintainVersion更新系统数据量接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
		return dataResponse;
	}*/

}
